package io.renren.modules.gasstation.realtimedata.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.common.utils.RedisKeys;
import io.renren.common.utils.RedisUtils;
import io.renren.modules.gasstation.realtimedata.dao.RealTimeDataDAO;
import io.renren.modules.gasstation.realtimedata.dto.RealTimeDataDTO;
import io.renren.modules.gasstation.realtimedata.entity.RealTimeDataEntity;
import io.renren.modules.gasstation.realtimedata.service.RealTimeDataService;
import io.renren.modules.sys.entity.SysUserEntity;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @Author：T-weiwei
 * @Description: 加油站实时数据业务实现层
 * @Email：tweiwei@aliyun.com
 * @CreateDate：2022-01-27-上午10:43
 * @CopyRight：http://www.huaxun.ink/
 * @Hint： This content is limited to the internal circulation of HuaXun Hi Tech Co., Ltd.,
 * and is forbidden to be leaked and used for other commercial purposes
 **/
@Service("realTimeDataService")
public class RealTimeDataServiceImpl extends ServiceImpl<RealTimeDataDAO, RealTimeDataEntity> implements RealTimeDataService {

    /**
     * 加油站实时数据数据层
     */
    @Resource
    private RealTimeDataDAO realTimeDataDAO;

    /**
     * redis工具类
     */
    @Resource
    private RedisUtils redisUtils;

    /**
     * 普通分页查询列表数据
     * @param params 参数
     * @return 分页对象
     */
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String enterpriseCode = (String)params.get("enterpriseCode");

        IPage<RealTimeDataEntity> page = this.page(
                new Query<RealTimeDataEntity>().getPage(params),
                new QueryWrapper<RealTimeDataEntity>()
                .eq(StringUtils.isNotBlank(enterpriseCode), "enterprise_code", enterpriseCode)
                .orderBy(true, false, "create_time")
        );

        return new PageUtils(page);
    }

    /**
     * 新的分页列表
     * @param params 参数
     * @return 分页工具类
     */
    @Override
    public PageUtils newQueryPage(Map<String, Object> params) {
        IPage<RealTimeDataDTO> page = new Query<RealTimeDataDTO>().getPage(params);
        params.put("pageParam", this.setPagingManually(params));
        // 优化后的分页查询列表
        List<RealTimeDataDTO> list = this.realTimeDataDAO.getOptimizedSQLStatementList(params);
        page.setRecords(list);
        // 获取该该企业编码下的数据总数
        String count = this.redisUtils.get(RedisKeys.getGasStationKeyByEnterpriseCode(params.get("enterpriseCode").toString()));
        page.setTotal(Long.valueOf(count));
        return new PageUtils(page);
    }

    /**
     * 修改分页参数
     * @param params 分页参数
     * @return 修改后的分页参数值
     */
    public Long setPagingManually(Map<String, Object> params) {
        Object pageO = params.get("pageParam");
        if (pageO != null) {
            Integer page = Integer.valueOf(pageO.toString());
            if (page.equals(1)) {
                return 0L;
            } else {
                Integer limit = Integer.valueOf(params.get("limitParam").toString());
                return Long.valueOf(page * limit - limit);
            }
        }
        return 0L;
    }
}
